<template>
  <div class="register-wrapper">
    <div class="register-container">
      <div class="form-header">
        <h2>创建账号</h2>
        <p>开启您的美好旅程</p>
      </div>
      <form @submit.prevent="handleRegister" class="floating-form">
        <div class="input-group">
          <input type="text" id="username" v-model="username" required maxlength="20" />
          <label for="username">用户名</label>
          <span class="highlight"></span>
        </div>
        <div class="input-group">
          <input type="tel" id="tele" v-model="tele" required maxlength="11" />
          <label for="tele">电话号码</label>
          <span class="highlight"></span>
        </div>
        <div class="input-group">
          <input type="password" id="password" v-model="password" required minlength="6" maxlength="20" />
          <label for="password">密码</label>
          <span class="highlight"></span>
        </div>
        <div class="input-group">
          <input type="password" id="confirmPassword" v-model="confirmPassword" required minlength="6" maxlength="20" />
          <label for="confirmPassword">确认密码</label>
          <span class="highlight"></span>
        </div>
        <button type="submit" class="submit-btn">
          <span>立即注册</span>
          <i class="arrow-icon"></i>
        </button>
        <div class="form-footer">
          <span>已有账号？</span>
          <a href="/login">立即登录</a>
        </div>
      </form>
    </div>

    <!-- 成功弹窗 -->
    <div v-if="showSuccessModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-content success">
          <h3>注册成功</h3>
          <p>您的账号已成功创建，即将跳转到登录页面...</p>
        </div>
      </div>
    </div>

    <!-- 失败弹窗 -->
    <div v-if="showErrorModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-content error">
          <h3>注册失败</h3>
          <p>{{ errorMessage }}</p>
          <button @click="showErrorModal = false" class="modal-close-btn">确定</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { userRegister } from '@/api/register';
import { useRouter } from 'vue-router';

const router = useRouter();

const username = ref('');
const tele = ref('');
const password = ref('');
const confirmPassword = ref('');
const showSuccessModal = ref(false);
const showErrorModal = ref(false);
const errorMessage = ref('');

const handleRegister = async () => {
  // ① 简单前端校验
  if (password.value !== confirmPassword.value) {
    errorMessage.value = '两次输入的密码不一致'
    showErrorModal.value = true
    return
  }

  // ② 组织请求体
  const registerData = {
    name: username.value,
    tele: tele.value,
    password: password.value,
    confirmPassword: confirmPassword.value
  }

  try {
    // ③ 调用封装好的 API（内部已带 baseURL、拦截器）
    await userRegister(registerData)

    // ④ 成功弹窗 & 3 秒后跳转
    showSuccessModal.value = true
    setTimeout(() => router.push('/login'), 3000)

  } catch (err) {

    // ⑤ 统一错误信息（request 拦截器已对业务 code≠0 做了处理）
    if (err.response) {
      errorMessage.value = err.response.data.message || '注册失败，请重试'
    } else if (err.request) {
      errorMessage.value = '网络错误，请检查您的连接'
    } else {
      errorMessage.value = '注册过程中发生错误'
    }
    showErrorModal.value = true
  }
}
</script>

<style scoped>
.register-wrapper {
  min-height: 93.5vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  padding: 20px;
  position: relative;
}

.register-container {
  width: 100%;
  max-width: 480px;
  background: white;
  border-radius: 20px;
  padding: 40px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
}

.form-header {
  text-align: center;
  margin-bottom: 40px;
}

.form-header h2 {
  color: #2c3e50;
  font-size: 32px;
  margin-bottom: 10px;
  font-weight: 700;
}

.form-header p {
  color: #95a5a6;
  font-size: 16px;
}

.floating-form .input-group {
  position: relative;
  margin-bottom: 30px;
}

.input-group input {
  width: 100%;
  padding: 15px;
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: transparent;
}

.input-group label {
  position: absolute;
  left: 15px;
  top: 50%;
  transform: translateY(-50%);
  background: white;
  padding: 0 5px;
  color: #95a5a6;
  font-size: 16px;
  transition: all 0.3s ease;
  pointer-events: none;
}

.input-group input:focus,
.input-group input:valid {
  border-color: #3498db;
}

.input-group input:focus+label,
.input-group input:valid+label {
  top: 0;
  font-size: 14px;
  color: #3498db;
}

.submit-btn {
  width: 100%;
  padding: 15px;
  background: linear-gradient(to right, #3498db, #2980b9);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}

.submit-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(52, 152, 219, 0.3);
}

.arrow-icon {
  border: solid white;
  border-width: 0 2px 2px 0;
  display: inline-block;
  padding: 3px;
  transform: rotate(-45deg);
}

.form-footer {
  text-align: center;
  margin-top: 20px;
  color: #95a5a6;
}

.form-footer a {
  color: #3498db;
  text-decoration: none;
  margin-left: 5px;
  font-weight: 600;
}

.form-footer a:hover {
  text-decoration: underline;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 12px;
  padding: 30px;
  width: 90%;
  max-width: 400px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  animation: modalFadeIn 0.3s ease-out;
}

.modal-content {
  text-align: center;
}

.modal-content h3 {
  margin-bottom: 15px;
  font-size: 24px;
}

.modal-content p {
  margin-bottom: 20px;
  color: #555;
}

.modal-content.success h3 {
  color: #27ae60;
}

.modal-content.error h3 {
  color: #e74c3c;
}

.modal-close-btn {
  padding: 10px 20px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.modal-close-btn:hover {
  background: #2980b9;
}

@keyframes modalFadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 480px) {
  .register-container {
    padding: 20px;
  }

  .form-header h2 {
    font-size: 24px;
  }

  .input-group input {
    padding: 12px;
  }

  .modal {
    padding: 20px;
  }
}
</style>